//
//  UuidAndName.swift
//  MTBLETools
//
//  Created by 方强 on 16/10/26.
//  Copyright © 2016年 MT. All rights reserved.
//

import UIKit
import FMDB
import MTSDK

class UuidAndName: NSObject {
    
    override init() {
        super.init();
    }
    
    init(name: String, uuid: String){
        super.init();
        
        self.name = name;
        self.uuid = uuid;
    }

    private var id:Int = 0;
    private var name: String!;
    private var uuid: String!;
    
    func setId(id:Int) {
        self.id = id;
    }
    func getId() -> Int {
        return id;
    }
    
    func setName(name: String){
        self.name = name;
    }
    func getName() -> String{
        return name;
    }
    func setUuid(uuid: String){
        self.uuid = uuid;
    }
    func getUuid() -> String{
        return uuid;
    }
    
    
    // 创建表
    public static func createTable(database:FMDatabase) -> Bool{
        guard database.open() else {
            print("Unable to open database");
            return false;
        }
        
        do {
            try database.executeUpdate("create table uuidandname(id integer primary key autoincrement, uuid text, name text)", values: nil);
        } catch {
            MTTools.logout(log: "failed: \(error.localizedDescription)");
            database.close();
            return false;
        }
        
        database.close();
        return true;
    }
    
    // 查询所有绑定设备
    public static func findAll(database:FMDatabase) -> [UuidAndName]?{
        guard database.open() else {
            MTTools.logout(log: "Unable to open database");
            return nil;
        }
        
        var bindList = [UuidAndName]();
        
        do {
            let rs = try database.executeQuery("select id, uuid, name from uuidandname", values: nil);
            
            while rs.next() {
                print("查出数据------------");
                let device = UuidAndName();
                device.id = Int(rs.int(forColumn: "id"));
                device.setUuid(uuid: rs.string(forColumn: "uuid"));
                device.setName(name: rs.string(forColumn: "name"));
                
                bindList.append(device);
            }
        } catch {
            print("failed: \(error.localizedDescription)");
            database.close();
            return nil;
        }
        
        database.close();
        return bindList;
    }
    
    // 添加设备
    public static func add(database:FMDatabase, device:UuidAndName) -> Int{
        guard database.open() else {
            MTTools.logout(log: "Unable to open database");
            return -1;
        }
        
        var newid = 0;
        
        print("插入设备============================");
        
        do {
            try database.executeUpdate("insert into uuidandname (uuid, name)  values (?, ?) ", values: [device.getUuid(), device.getName()]);
            
            newid = Int(database.lastInsertRowId());
        } catch {
            print("failed: \(error.localizedDescription)");
            database.close();
        }
        
        database.close();
        return newid;
    }
    
    // 更新设备
    public static func updata(database:FMDatabase, device:UuidAndName) -> Bool{
        guard database.open() else {
            MTTools.logout(log: "Unable to open database");
            return false;
        }
        
        do {
            try database.executeUpdate("update uuidandname set uuid = ?, name = ?  WHERE id = ? ", values: [device.getUuid(), device.getName(),NSNumber(value: device.id)]);
        } catch {
            print("failed: \(error.localizedDescription)");
            database.close();
            return false;
        }
        
        database.close();
        return true;
    }
    
    // 删除设备
    public static func delete(database:FMDatabase, device:UuidAndName) -> Bool{
        guard database.open() else {
            MTTools.logout(log: "Unable to open database");
            return false;
        }
        
        do {
            try database.executeUpdate("delete from uuidandname where id = ?", values: [NSNumber(value: device.id)]);
        } catch {
            print("failed: \(error.localizedDescription)");
            database.close();
            return false;
        }
        
        database.close();
        return true;
    }
    
}
